package com.shop2cn.iapi.wxcomponent.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.wxcomponent.parameter.ConfirmAuditMchAppBean;
import com.shop2cn.iapi.wxcomponent.service.ConfirmAuditMchAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.Auditrecord;
import com.ymttest.database.model.Mchapp;
import com.ymttest.database.model.Platformconfig;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
*小程序提审
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ConfirmAuditMchApp{
	private static ConfirmAuditMchAppBean confirmauditmchappBean;
	private static ConfirmAuditMchAppCall confirmauditmchappCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("小程序提审");
	}

	@Before
	public void caseUp() {
		confirmauditmchappBean=new ConfirmAuditMchAppBean();
		confirmauditmchappCall=new ConfirmAuditMchAppCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmauditmchappCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_001() {
		Logger.start(true,"小程序提审");
		try {
			Integer mchid=9001;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Normal");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("200", confirmauditmchappCall.getString("code"), "验证返回code");
			//插入记录 AuditRecord
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			List<Auditrecord> list = DefGodMchWapper.selectAuditrecordByExample(mchid);
			Integer auditRecordId=0;
			if(list!=null&&!list.isEmpty()) {
				Auditrecord audit = list.get(0);
				auditRecordId = audit.getAuditRecordId();
				Logger.verifyEquals(auditid, audit.getAuditId(), "验证auditid");
				Logger.verifyEquals(appid, audit.getAppId(), "验证appid");
				// mock getTemplateList release info
				Logger.verifyEquals("1", audit.getTemplateId(), "验证TemplateId");
				Logger.verifyEquals("autotest1", audit.getVersion(), "验证Version");
			}
			
			List<Mchapp> mlist = DefGodMchWapper.selectMchappByExample(mchid);
			if(mlist!=null&&!mlist.isEmpty()) {
				Mchapp mchapp = mlist.get(0);
				// 2:审核中
				Logger.verifyEquals(2, mchapp.getAppStatus(), "验证AppStatus");
				Logger.verifyEquals(auditRecordId, mchapp.getAuditRecordId(), "验证auditRecordId");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_002() {
		Logger.start(true,"不输入mchid失败");
		try {
			confirmauditmchappCall.setData(confirmauditmchappBean);
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_003() {
		Logger.start(true,"输入mchid错误失败");
		try {
			confirmauditmchappBean.setMchId(-1);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_004() {
		Logger.start(true,"小程序提审-uploadCode-fail失败");
		try {
			Integer mchid=9001;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Fail");
			m.put("mockParam-2-code",-1);
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Normal");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
			Logger.verifyIsContains("上传代码失败", confirmauditmchappCall.getString("message"), "验证返回message");
			//插入记录 AuditRecord
			//
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_005() {
		Logger.start(true,"小程序提审-submitAudit-Fail");
		try {
			Integer mchid=9001;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Fail");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Normal");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
			Logger.verifyIsContains("提交审核失败", confirmauditmchappCall.getString("message"), "验证返回message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_006() {
		Logger.start(true,"小程序提审-getTemplateList-Fail");
		try {
			Integer mchid=9001;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Fail");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
			Logger.verifyIsContains("获取最新模板列表失败", confirmauditmchappCall.getString("message"), "验证返回message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_007() {
		Logger.start(true,"小程序提审-getTemplateList-NoRelease");
		try {
			Integer mchid=9001;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-NoRelease");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("500", confirmauditmchappCall.getString("code"), "验证返回code");
			Logger.verifyIsContains("No value present", confirmauditmchappCall.getString("message"), "验证返回message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_008() {
		Logger.start(true,"小程序提审-getCategory-Fail");
		try {
			Integer mchid=9001;
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Normal");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Fail");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
			Logger.verifyIsContains("获取审核时可用类目信息失败", confirmauditmchappCall.getString("message"), "验证返回message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_009() {
		Logger.start(true,"小程序提审-重复提交");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Normal");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("200", confirmauditmchappCall.getString("code"), "验证返回code");
			//插入记录 AuditRecord
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			List<Auditrecord> list = DefGodMchWapper.selectAuditrecordByExample(mchid);
			Integer auditRecordId=0;
			if(list!=null&&!list.isEmpty()) {
				Auditrecord audit = list.get(0);
				auditRecordId = audit.getAuditRecordId();
				Logger.verifyEquals(auditid, audit.getAuditId(), "验证auditid");
				Logger.verifyEquals(appid, audit.getAppId(), "验证appid");
				// mock getTemplateList release info
				Logger.verifyEquals("1", audit.getTemplateId(), "验证TemplateId");
				Logger.verifyEquals("autotest1", audit.getVersion(), "验证Version");
			}
			
			List<Mchapp> mlist = DefGodMchWapper.selectMchappByExample(mchid);
			if(mlist!=null&&!mlist.isEmpty()) {
				Mchapp mchapp = mlist.get(0);
				// 2:审核中
				Logger.verifyEquals(2, mchapp.getAppStatus(), "验证AppStatus");
				Logger.verifyEquals(auditRecordId, mchapp.getAuditRecordId(), "验证auditRecordId");
			}
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
			Logger.verifyIsContains("已存在正在审核的版本", confirmauditmchappCall.getString("message"), "验证返回message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuditMchApp_010() {
		Logger.start(true,"小程序提审,未付费--成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			
			confirmauditmchappBean.setMchId(mchid);
			confirmauditmchappCall.setData(confirmauditmchappBean);
			
			Map m=new HashMap();
//			mock:mock-enable
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "wxcomponent-uploadCode-Normal");
			m.put("mockId-2", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-2", "wxcomponent-submitAudit-Normal");
			m.put("mockId-3", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-3", "wxcomponent-getTemplateList-Normal");
			m.put("mockId-4", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-4", "wxcomponent-getCategory-Normal");
			m.put("mockId-5", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-5", "wxcomponent-release-Normal");
			String auditid=TestDataManager.getRandomNum(6);
			m.put("mockParam-1-code",0);
			m.put("mockParam-2-auditid",auditid);
			PayGateWayCallService.addMock(confirmauditmchappCall, m);
			
			confirmauditmchappCall.callService();			
			Logger.verifyEquals("400", confirmauditmchappCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_ConfirmAuditMchApp_011() {
		Logger.start(true,"授权-付费-提审--取消授权--授权-提审-判断撤回状态");
		try {
			DefGodMchWapper defGodMchWapper = new DefGodMchWapper();
			//授权-付费提审
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			WxComponentService.deleteMchinfo(mchid);
			WxComponentService.ApplyCharge(mchid);
			WxComponentService.ChargeNotify(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ConfirmAuditMchApp(mchid);
			Auditrecord auditrecord = defGodMchWapper.selectAuditrecordByExample(mchid).get(0);
			//取消授权--再次授权提审
			WxComponentService.CancelAuthorization(mchid);
			String appid1 = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid1, "authorized");
			WxComponentService.ConfirmAuditMchApp(mchid);
			Auditrecord auditrecord1 = defGodMchWapper.selectAuditrecordByExample(mchid).get(0);
			List<Mchapp> selectMchappBycount = defGodMchWapper.selectMchappByExample(mchid);
			
			Logger.verifyEquals(appid1, selectMchappBycount.get(0).getAppId(), "审核第二次的提审的小程序");
			
			System.out.println(auditrecord);
			System.out.println(auditrecord1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}